package com.example.boot_usb.util;

import com.example.boot_usb.pojo.Student;
import io.jsonwebtoken.*;
import lombok.val;
import org.junit.Test;

import java.util.Date;
import java.util.UUID;

public class jwtUtil {

    public long time = 1000*60*60*24;
    public String signature = "xian";

    @Test
    public void jwt(){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
                //header
                //.setHeaderParam("typ","JWT")
                //.setHeaderParam("alg","HS256")
                //payload
                .claim("studnetName","闲闲")
                .claim("studnet",1)
                .setSubject("student-jwt")
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();
        System.out.println(jwtToken);
    }



    @Test
    public void test(){
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdHVkbmV0TmFtZSI6IumXsumXsiIsInN0dWRuZXQiOjEsInN1YiI6InN0dWRlbnQtand0IiwiZXhwIjoxNjM5NjQ1Njk4LCJqdGkiOiJhNWEzYzk4OC00ZDgzLTQ1OWYtYjVhNi01OGE0M2FjNGI1Y2IifQ.8TZ-xFOUqEVZ_Z5GLrTlzLPki8xUil7pjthD3OWPm9Q";
        JwtParser parser = Jwts.parser();
        Jws<Claims> claimsJws = parser.setSigningKey(signature).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        Object parser1 = body.get("studnetName");
        Object aa = body.get("studnet");
        System.out.println(aa);
        System.out.println(parser1);


    }

}
